package com.slipper.system.codeless.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.system.codeless.bo.VisualTemplateBO;
import com.slipper.system.codeless.dao.VisualTemplateDao;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@ReadTransactional
public class VisualTemplateService {
    @Resource
    private VisualTemplateDao visualTemplateDao;
    
    public VisualTemplateBO queryBy(String id) {
        return visualTemplateDao.queryBy(id);
    }
    
    public List<VisualTemplateBO> queryList(VisualTemplateBO bo) {
        return visualTemplateDao.queryList(bo);
    }

    public PageInfo<Map<String, String>> queryCategoryPage(VisualTemplateBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(visualTemplateDao.queryCategoryList());
    }

    public PageInfo<VisualTemplateBO> queryPage(VisualTemplateBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(visualTemplateDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(VisualTemplateBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            return visualTemplateDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setCreateTime(System.currentTimeMillis());
            return visualTemplateDao.insert(bo);
        }
    }

    @WriteTransactional
    public int saveSimple(VisualTemplateBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            return visualTemplateDao.updateSimple(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setCreateTime(System.currentTimeMillis());
            return visualTemplateDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(VisualTemplateBO bo) {
        return visualTemplateDao.delete(bo);
    }
}
